/*
 * @Descripttion:
 * @Author: Silence
 * @Date: 2023-05-27 19:30:11
 * @LastEditors: Silence
 * @LastEditTime: 2023-05-28 21:36:15
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeItem from '../work/HomeItem.vue'
import Login from '../components/Login.vue'
import Register from '../components/Register.vue'
import FirstBox from '../work/FirstBox.vue'
import FindBox from '../work/FindBox.vue'
import InfoBox from '../work/InfoBox.vue'
import MyBox from '../work/MyBox.vue'
import NotFound from '../views/NotFound.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeItem,
    redirect: '/first',
    children: [
      {
        path: '/first',
        name: 'FirstBox',
        component: FirstBox
      },
      {
        path: '/find',
        name: 'FindBox',
        component: FindBox,
        //独享守卫
        beforeEnter: (to, from, next) => {
          console.log(111)
          return next()
        }
      },
      {
        path: '/info',
        name: 'InfoBox',
        component: InfoBox
      },
      {
        path: '/my',
        name: 'MyBox',
        component: MyBox
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '*',
    name: 'NotFound',
    component: NotFound
  }
]

const router = new VueRouter({
  routes
})
//全局守卫
router.beforeEach((to, from, next) => {
  //当进入路径为'/my'时跳转登录页面,否则直接跳转,
  //通过meta属性设置可以用来判断用户是否登录,没有登录直接跳转到登录界面
  if (to.path == '/my') {
    return next('/login')
  } else {
    next()
  }
})
router.beforeResolve((to, from, next) => {
  console.log(222)
  return next()
})
router.afterEach((to, from) => {
  console.log(333)
})
export default router
